import java.io.BufferedReader;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class pubMedConverter extends pubMedConverter_analysis{

	JFileChooser fc;
	JPanel panel;

	

	
	public pubMedConverter(boolean useNonTagged) {
		super(useNonTagged);
		fc = new JFileChooser();
		panel = new JPanel();
		authors = new Hashtable<String,Integer>();

		
		
	}
	

	
	public void start(){
		
		boolean running = true;
		
		while(running){
			int returnVal = fc.showOpenDialog(panel);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
               File file = fc.getSelectedFile();
				System.out.println("open "+file.getName());
				String newFile = start_1_cleanFile(file);
				start_1_onFile(new File(newFile));
				removeFile(newFile);
				output(file);
				JOptionPane.showMessageDialog(panel, "All Done... I love you Marina");
            } else {
                //log.append("Open command cancelled by user." + newline);
            	running = false;
            }
		}		
	}
	
	
	public void output(File file){
		File parent = new File(file.getParent());
        
        String parentPath = parent.getAbsolutePath();//+"/_"+file.getName();
        String outputPath = parentPath+"/"+file.getName()+"_";
        File dir = new File(outputPath);
        dir.mkdir();
        
        printToFile(dir.getAbsolutePath()+"/author.txt",authors);
        printToFile(dir.getAbsolutePath()+"/conf.txt",journals);
        printToFileReverse(dir.getAbsolutePath()+"/paper.txt",papers);
        printToFile(dir.getAbsolutePath()+"/term.txt",terms);
        
        printPapersToFile(dir.getAbsolutePath()+"/paper_author.txt",paper_authors);
        printPapersToFile(dir.getAbsolutePath()+"/paper_conf.txt",paper_journal);
        printPapersToFile(dir.getAbsolutePath()+"/paper_term.txt",paper_terms);
        
        
	}
	
	public void printToFileReverse(String file, Hashtable<Integer,String> values){
		//String newline = System.getProperty("line.separator");
		String newline = "\r\n";
		BufferedWriter output;
		try {
			output = new BufferedWriter(new FileWriter(new File(file)));
			Set<Integer> keys = values.keySet();
			
			for(Integer key:keys)
				output.write(key+"\t"+values.get(key)+newline);			
			
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void printToFile(String file, Hashtable<String,Integer> values){
		//String newline = System.getProperty("line.separator");
		String newline = "\r\n";
		BufferedWriter output;
		try {
			output = new BufferedWriter(new FileWriter(new File(file)));
			Set<String> keys = values.keySet();
			
			for(String key:keys)
				output.write(values.get(key)+"\t"+key+newline);			
			
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void printPapersToFile(String file, Hashtable<Integer, ArrayList<Integer>> values){
		//String newline = System.getProperty("line.separator");
		String newline = "\r\n";
		BufferedWriter output;
		try {
			output = new BufferedWriter(new FileWriter(new File(file)));
			Set<Integer> keys = values.keySet();
			
			for(Integer key:keys){
				ArrayList<Integer> vals = values.get(key);
				for(Integer v:vals)
					output.write(key+"\t"+v+newline);
				
			}
			
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		pubMedConverter pmc = new pubMedConverter(false);
		pmc.start();

	}

}
